package com.gzgy.ustore.api.web.controller.webservice;


import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.util.Constant;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.common.util.PushUtil;
import com.gzgy.ustore.api.service.PushService;
import com.gzgy.ustore.api.util.ResultUtil;
import com.gzgy.ustore.api.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class PushController  extends BaseController{

    @Autowired
    private PushService pushService;

    @RequestMapping(value = "/pushMessage",method = RequestMethod.POST)
    public String pushMessage(ModelMap modelMap ,String title ,String message, int flag,String dvcToken){
        Assert.hasText(title,"title " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(message,"message " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.notNull(flag,"flag " + I18nUtil.getMessage(-2,null,"zh_CN"));
        String result = "";
        if(flag == 0){
           result = ResultUtil.getResultJson(pushService.pushMessage(title,message));
        }else if(flag == 1){
            Assert.hasText(dvcToken,"dvcToken " + I18nUtil.getMessage(-2,null,"zh_CN"));
            result = ResultUtil.getResultJson(pushService.pushMessage(title,message,dvcToken));
        }else{
            throw new BaseAPIException("发送错误");
        }

        modelMap.put("message",result);

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "/pushMessageList",method = RequestMethod.POST)
    public String pushMessageList(ModelMap modelMap,Integer index,Integer page,Integer type ,String deviceToken){
        Assert.notNull(index,"index " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.notNull(page,"page " + I18nUtil.getMessage(-2,null,"zh_CN"));

        if(type!=null && type ==1 ){
            Assert.hasText(deviceToken,"deviceToken " + I18nUtil.getMessage(-2,null,"zh_CN"));
        }
        modelMap.put("message",ResultUtil.getResultJson(pushService.pushMessageList(index,page,type,deviceToken)));
        return SHOW_JSON_NAME;
    }
}
